package com.whiteblue.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.whiteblue.Interceptor.LoginInterceptor;
import com.whiteblue.model.Book;
import com.whiteblue.model.User;
import com.whiteblue.validator.EditInfoValidator;
import com.whiteblue.validator.LoginValidator;
import com.whiteblue.validator.RegisterValidator;

/**
 * Created by WhiteBlue on 15/4/4.
 */
public class UserController extends Controller {

    public void info() {
        if (getParaToInt("user") == null) {
            this.renderError(404);
        }
        int userId = getParaToInt("user");
        User user = User.dao.getById(userId);
        this.setAttr("user", user);
        this.render("/user_info.html");
    }


    @Before(LoginInterceptor.class)
    public void myInfo() {
        User user = this.getSessionAttr("user");
        this.setAttr("user", user);
        this.render("/user_info.html");
    }

    @Before({LoginInterceptor.class, EditInfoValidator.class})
    public void editInfo() {
        User user = this.getSessionAttr("user");
        user.editInfo(getPara("feeling"), getPara("location"));
        this.setSessionAttr("user", user);
        this.getSession().setMaxInactiveInterval(3600);
        this.setSessionAttr("info", "修改成功");
        this.redirect("/");
    }


    public void logout() {
        if (this.getSessionAttr("user") == null) {
            this.setAttr("msg", "然而你并没有登陆");
            this.render("/info.html");
        } else {
            this.removeCookie("user");
            this.removeSessionAttr("user");
            this.setSessionAttr("info", "注销成功");
            this.redirect("/");
        }
    }


    @Before(LoginValidator.class)
    public void login() {
        if (this.getSessionAttr("user") != null) {
            this.setAttr("msg", "你已登陆");
            this.render("/info.html");
        } else {
            User user = User.dao.login(getPara("email"), getPara("password"));
            if (user != null) {
                this.setCookie("user", user.getStr("email") + "-" + user.getStr("password"), 3600 * 24 * 30);
                this.setSessionAttr("user", user);
                this.getSession().setMaxInactiveInterval(3600);
                this.setSessionAttr("info", "登陆成功");
                this.redirect("/");
            } else {
                this.setAttr("msg", "用户名或密码错误");
                this.render("/info.html");
            }
        }
    }

    @Before(RegisterValidator.class)
    public void register() {
        if (this.getSessionAttr("user") != null) {
            this.setAttr("msg", "你已登陆");
            this.render("/info.html");
        } else {
            if (User.dao.find(getPara("email"), getPara("name"))) {
                this.setAttr("msg", "邮箱或昵称已存在");
                this.render("/info.html");
            } else {
                User user = getModel(User.class);
                user.register(getPara("email"), getPara("password"), getPara("name"));
                this.setCookie("user", user.getStr("email") + "-" + user.getStr("password"), 3600 * 24 * 30);
                this.setSessionAttr("user", user);
                this.getSession().setMaxInactiveInterval(3600);
                this.setSessionAttr("info", "注册成功");
                this.redirect("/");
            }
        }
    }

    @Before(LoginInterceptor.class)
    public void checkBook(){
        if (getParaToInt("user") == null) {
            this.renderError(404);
        }else{
            User user=User.dao.getById(getParaToInt("user"));
            if(user==null) {
                this.renderError(404);
            }else{
                this.setAttr("page",Book.dao.getForUser(user.getInt("id"),getParaToInt("page",1)));
                this.setAttr("actionUrl","/user/checkBook?user="+user.getInt("id") + "&page=");
                this.render("/user_list.html");
            }
        }
    }

    @Before(LoginInterceptor.class)
    public void checkMsg(){
        User user=getSessionAttr("user");
        Page<Book> page= Book.dao.getMsgList(user.getInt("id"),getParaToInt("page",1));
        this.setAttr("page",page);
        this.setAttr("actionUrl","/user/checkMsg?" + "page=");
        this.render("/return_list.html");
    }
}
